﻿using MvcContrib;
using System.Web.Mvc;
using DecisionMaking.BusinessLogic;
using DecisionMaking.BusinessLogic.Model;

namespace DecisionMaking.WebApp.Controllers
{
    [HandleError]
    [Authorize]
    public class AgentsController : BaseController
    {
        /// <summary>
        /// Injected by Windsor 
        /// </summary>
        public IEnvironmentManager EnvironmentManager { private get; set; }	

        [HttpGet]
        public ActionResult List(int id)
        {
            var environment = EnvironmentManager.GetEnvironment(id);
            // make sure current user is owner of current environment
            if (!environment.Account.Equals(CurrentAccount))
            {
                return AccessDenied();
            }

            ViewData["environment"] = environment;
            ViewData["agents"] = EnvironmentManager.GetAgents(id);

            return View(new AgentsModel { EnvironmentId = id });
        }

        [HttpPost]
        public ActionResult Generate(AgentsModel model)
        {
            if (ModelState.IsValid)
            {
                EnvironmentManager.GenerateAgents(model.EnvironmentId.GetValueOrDefault(), model.Amount.GetValueOrDefault());

                return this.RedirectToAction(c => c.List(model.EnvironmentId.GetValueOrDefault()));
            }

            return List(model.EnvironmentId.GetValueOrDefault());
        }

    }
}
